package cn.edu.neu.vmmigeval.io;

import com.sun.org.apache.regexp.internal.RE;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ReadUtil {
	
	
	public static Map<Integer,List<Integer>> readFromMappingFile(String url) throws Exception{
		
		
		Map<Integer,List<Integer>> map = new HashMap<Integer, List<Integer>>();
	        File file = new File(url);
	        BufferedReader reader = null;
	        String str = null;
	        try {
	        	//   System.out.println("以行为单位读取文件内容，一次读一整行：");
//	            reader = new BufferedReader(new FileReader(file));
				reader = new BufferedReader(new InputStreamReader(ReadUtil.class.getResourceAsStream(url)));
	            
	            List<Integer> lists =  null;
	            while ((str = reader.readLine()) != null) {
	            	String[] str2 = null;
	            	lists = new ArrayList<Integer>();
	            	String[] str1 = str.split(":");
	            	int id = Integer.parseInt(str1[0]);
	            	
	            	if(str1.length!=1){
	            	
		            	str2 = str1[1].split(" ");
		            	if(str2!=null){
		            		for (String string : str2) {
		            			lists.add(Integer.parseInt(string));
							}
		            	}
	            	
	            	}
	            	map.put(id, lists);
	            	
	            }
	            reader.close();
	    	}catch(Exception e){
	    		e.printStackTrace();
	    	}
	        return map;
	      
	}

	
	
}
